﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Fcdbas.WinUI.Forms;

using Fcdbas.Platform.Interface;
using Fcdbas.WinUI.Common;
using Fcdbas.Core.Windows;
using Fcdbas.WinUI.Windows;
using Fcdbas.Platform.Properties;
using Fcdbas.Platform;
using Fcdbas.WinUI.Controls;
using Fcdbas.Core;

namespace Fcdbas.WinUI.Forms
{
    public partial class dlgChangePassword : cDialogWindow
    {
        public dlgChangePassword()
        {
            InitializeComponent();
        }

        private void dlgChangePassword_Load(object sender, EventArgs e)
        {
            this.txtUserID.Text = AppEnvironment.Session.USER_ID;
            this.txtUserName.Text = AppEnvironment.Session.USER_ID;
            this.txtUserID.ReadOnly = true;
            this.txtUserName.ReadOnly = true;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(txtOldPsw.Text ))
            {
                this.ShowMessage("必须输入旧密码才能设置新密码！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (txtOldPsw.Text != AppEnvironment.Session.USER_PWD)
            {
                this.ShowMessage("旧密码输入错误！", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtOldPsw.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtNewPsw.Text) || string.IsNullOrEmpty(txtPswAgain.Text))
            {
                this.ShowMessage("新密码没有输入！", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtNewPsw.Focus();
                return;
            }
            if (txtNewPsw.Text != txtPswAgain.Text)
            {
                this.ShowMessage("两次输入的密码不匹配，请重新输入！", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtPswAgain.Focus();
                return;
            }

            cSessionManager.DbTransactionBegin();
            Dictionary<SalString, object> dic = new Dictionary<SalString, object>();
            dic.Add("user_id_", txtUserID.Text);
            dic.Add("old_password_", txtOldPsw.Text );
            dic.Add("new_password_", txtNewPsw.Text);

            SalBoolean res = cSessionManager.DbMySQLBlock("FndSession_ChgPassword(:user_id_,:old_password_,:new_password_)", dic);
            if (res)
            {
               
                cSessionManager.DbTransactionEnd();
                AppEnvironment.Session.USER_PWD = txtNewPsw.Text;
                this.ShowMessage("密码修改成功，请记住新密码！", "");
            }
            else
            {
                cSessionManager.DbTransactionClear();
                SalString sRes = "";
                cSessionManager.GetSqlLastError(ref sRes);
                WinTranslate.GetTranslate().ShowMessage(sRes, "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
            this.Close();
        }
    }
}
